package com.zjsx.lib.utils;


import java.io.File;
import java.io.IOException;
import java.util.Locale;

import android.os.Environment;

public class FileUtils {
    /**
     * 判断目录是否存在，不在则创建
     * */
    public static File getDirectory(String directory) {
	File file = new File(directory);
	if (!file.exists()) {
	    file.mkdirs();
	}
	L.d("get folder : "+directory+"-file exist : " + file.exists());
	return file;
    }
    
    public static void createFile(String path){
	File file = new File(path);
	if(!file.exists()){
	    try {
		file.createNewFile();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
    }
    
    /**
     * 检查SD卡是否已装载
     * */
    public static boolean isExistSdCard() {
	return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }
    
    /**
     * 获得SD目录路径
     * */
    public static File getSD() {
	return Environment.getExternalStorageDirectory();
    }
    
    public static String getSDPath(){
	return getSD().getAbsolutePath();
    }
    
    /**
     * 去掉文件后缀
     * */
    public static String clearSuffix(String str) {
	int i = str.lastIndexOf(".");
	if (i != -1) {
	    return str.substring(0, i);
	}
	return str;
    }
    
    /**
     * 获取文件的后缀名，返回小写
     * */
    public static String getSuffix(String str) {
	int i = str.lastIndexOf('.');
	if (i != -1) {
	    return str.substring(i + 1).toLowerCase(Locale.ENGLISH);
	}
	return str;
    }
    
    /**
     * 根据文件路径获取文件名
     * @param path
     * @return
     */
    public static String getFileNameByPath(String path) {
	String fileName = null;
	int i = path.lastIndexOf("/");
	if (i != -1) {
	    fileName = path.substring(i + 1, path.length());
	}
	return fileName;
    }
    
    
}
